#!/usr/bin/env node

const http = require('http'),
      fs = require('fs'),
      log = console.log;

http.createServer((req,res)=>{
  
  if(req.url !== '/'){
    res.statusCode=404;
    res.setHeader('content-type','text/html');
    res.end('<h1>Not found</h1>');
    return;
  }

  let data = '';
  req.setEncoding('binary');
  req.on('data', chunck => data+=chunck);
  req.on('end', () => {
    data = data.split('\r\n');
    let filename = data[1].split('; ')[2].split('=')[1];
    filename = filename.slice(1,filename.length-1);
    log(filename);
    log(data[4]);
    
    fs.writeFileSync(filename,data[4],'binary');
    res.end('OK');
  });
}).listen(8080);
